// pages/add/addcontent/addcontent.js
//引入工具类
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:"",
    phone:"",
    idCard:"",
    area:"",
    isShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

   //获取用户输入的用户名
   userNameInput:function(e){
     this.setData({
       userName: e.detail.value
     })
   },
   //获取用户手机号码
   phoneInput:function(e){
    this.setData({
      phone: e.detail.value
    })
   },
   //获取身份证号
   idCardInput:function(e){
    this.setData({
      idCard: e.detail.value
    })
   },
   //获取所在地区
   areaInput:function(e){
    this.setData({
      area: e.detail.value
    })
   },
   //检验用户名
   checkUserName:function(e){
    //获取用户输入的姓名
    let inputUserName  = this.data.userName;
    if(/^[\u4e00-\u9fa5]{2,4}$/.test(inputUserName)){
      wx.setStorageSync('userName', inputUserName);
      return true;
    }else{
      wx.showToast({
        title: '请输入20位以内中文姓名，请勿输入空格',
        icon: 'none'
      });
      return false;
    }
  },
  //检验手机号
  checkPhone:function(e){
    //获取用户输入的手机号
    let inputPhone = this.data.phone;
    if(/^1[3-9]\d{9}$/.test(inputPhone)){
      wx.setStorageSync('phone', inputPhone);
      return true;
    }else{
      wx.showToast({
        title: '请填写正确的手机号码',
        icon: 'none'
      });
      return false;
    }   
  },
  //检验身份证号
  checkIdCard:function(e){
    //获取用户输入的身份证号
    let inputIdCard = this.data.idCard;
    if(/^[1-8][1-7]\d{4}(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dX]$/
    .test(inputIdCard)){
      wx.setStorageSync('idCard', inputIdCard);
      return true;
    }else{
      wx.showToast({
        title: '请填写正确的身份证号',
        icon: 'none'
      });
      return false;
    }
  },
  //检验地区
  checkArea:function(e){
    //获取用户输入的所在地区
    let inputArea = this.data.area;
    if(/^[\u4e00-\u9fa5]{2,20}$/.test(inputArea)){
      wx.setStorageSync('area', inputArea);
      return true;
    }else{
      wx.showToast({
        title: '请正确填写所在地区',
        icon: 'none'
      });
      return false;
    }
  },

  //姓名输入框失去焦点事件
  nameInputBlur(e){
    this.checkUserName(e);
    //声明对象
    const app = getApp();
    //设置全局变量
    app.globalData.nameStatus = this.checkUserName(e);
    console.log(app.globalData); 
    if(app.globalData.nameStatus && app.globalData.phoneStatus && app.globalData.idCardStatus 
      && app.globalData.areaStatus){
        this.setData({
          isShow: true
        })
    }else{
      this.setData({
        isShow: false
      })
    }
  },
  //手机号输入框失去焦点事件
  phoneInputBlur(e){
    this.checkPhone(e);
    //声明对象
    const app = getApp();
    //设置全局变量
    app.globalData.phoneStatus = this.checkPhone(e);
    console.log(app.globalData);
    if(app.globalData.nameStatus && app.globalData.phoneStatus && app.globalData.idCardStatus 
      && app.globalData.areaStatus){
        this.setData({
          isShow: true
        })
    }else{
      this.setData({
        isShow: false
      })
    }
  },

  //身份证号输入框失去焦点事件
  idCardInputBlur(e){
    this.checkIdCard(e);
    //声明对象
    const app = getApp();
    //设置全局变量
    app.globalData.idCardStatus = this.checkIdCard(e);
    console.log(app.globalData);
    if(app.globalData.nameStatus && app.globalData.phoneStatus && app.globalData.idCardStatus 
      && app.globalData.areaStatus){
        this.setData({
          isShow: true
        })
    }else{
      this.setData({
        isShow: false
      })
    }
  },
  //地区输入框失去焦点事件
  areaInputBlur(e){
    this.checkArea(e);
    //声明对象
    const app = getApp();
    //设置全局变量
    app.globalData.areaStatus = this.checkArea(e);
    console.log(app.globalData);
    if(app.globalData.nameStatus && app.globalData.phoneStatus && app.globalData.idCardStatus 
      && app.globalData.areaStatus){
        this.setData({
          isShow: true
        })
    }else{
      this.setData({
        isShow: false
      })
    }
  },
  //点击提交
  btnClick(e){
    let that = this;
    let userName = wx.getStorageSync('userName');
    let phone = wx.getStorageSync('phone');
    let idCard = wx.getStorageSync('idCard');
    let area = wx.getStorageSync('area');
    let status = 1;
    console.log("姓名，手机号，身份证，地区",userName,phone,idCard,area);
    let openid = wx.getStorageSync('openId');
    console.log(openid);
    
    //如果openid 不为空
    if(openid != null || openid != ""){
      
      wx.request({
        url: 'http://localhost:8060/user/amend',
        method: 'POST',
        header: { "content-type": "application/x-www-form-urlencoded" },
        data: {
          openid,
          userName,
          phone,
          idCard,
          area,
          status
        },
        success(responseData){
          let result = responseData.data.updateStatus;
          wx.setStorageSync('userName3',that.data.userName);
          wx.setStorageSync('phone3',that.data.phone);
          console.info(that.data.userName,that.data.phone)
          console.info(result)
        }
      });
      wx.showToast({
        title: '提交中',
        icon: 'loading',
        duration: 1000
      });
      //成功后跳转页面
      wx.switchTab({
        url: '/pages/my/my',
      });
      //给用户显示成功后响应
      wx.showToast({
        title:  '添加成功',
        icon: 'success'
      })
    }else{
      wx.showToast({
        title: '错误',
        icon: 'none'
      })

    }
    

  }
  
})